'use strict';

angular.module('app.view.fan-data-problem')
    .config(['$stateProvider', function($stateProvider) {
        $stateProvider.state('statistics', {
            url: '/views/fan-data-problem/statistics',
            views: {
                'navigation': {
                    templateUrl: 'views/header/navigation.html',
                    controller: 'navigationCtrl'
                },
                'sidebar': {
                    templateUrl: 'views/sidebar/sidebar-fan-data-problem.html',
                    controller: 'sidebarCtrl'
                },
                'content': {
                    templateUrl: 'views/fan-data-problem/statistics/statistics.html',
                    controller: 'statisticsCtrl'
                }
            }
        })
    }])

    .controller('statisticsCtrl', ['$scope', 'Upload', 'communicationService',
        function($scope, Upload, communicationService) {

            $scope.data = [{incomplete: [0, 1], inaccurate: [0, 1], inconsistent: [0, 1] }];
            var incompleteChart = echarts.init(document.getElementById("incomplete"));
            var inaccurateChart = echarts.init(document.getElementById("inaccurate"));
            var inconsistentChart = echarts.init(document.getElementById("inconsistent"));


            var incompleteOption = {
                title : {
                    text: '不完整数据统计',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    right: 'right',
                    bottom:'bottom',
                    data: ['不完整数据','正常数据']
                },
                series : [
                    {
                        name: '数据不完整问题',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'不完整数据'},
                            {value:1, name:'正常数据'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            var inaccurateOption = {
                title : {
                    text: '不精确数据统计',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    right: 'right',
                    bottom:'bottom',
                    data: ['不精确数据','正常数据']
                },
                series : [
                    {
                        name: '数据不精确问题',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'不精确数据'},
                            {value:1, name:'正常数据'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            var inconsistentOption = {
                title : {
                    text: '不一致列统计',
                    x:'center'
                },
                tooltip : {
                    trigger: 'item',
                    formatter: "{a} <br/>{b} : {c} ({d}%)"
                },
                legend: {
                    orient: 'vertical',
                    right: 'right',
                    bottom:'bottom',
                    data: ['不一致列','正常列']
                },
                series : [
                    {
                        name: '数据不一致问题',
                        type: 'pie',
                        radius : '55%',
                        center: ['50%', '60%'],
                        data:[
                            {value:0, name:'不一致列'},
                            {value:1, name:'正常列'},
                        ],
                        itemStyle: {
                            emphasis: {
                                shadowBlur: 10,
                                shadowOffsetX: 0,
                                shadowColor: 'rgba(0, 0, 0, 0.5)'
                            }
                        }
                    }
                ]
            };

            // 每隔10s请求后台数据，更新统计信息
            function post() {
                communicationService.post('/api/fan-data-problem/statistics', {
                    operation: "query"
                }).then(function(data){
                    let temp = [];

                    // console.log("query");
                    // console.log(data);

                    temp.push({
                        incomplete: [data[0].key, data[0].key + data[0].value === 0? 1 :data[0].value],
                        inaccurate: [data[1].key, data[1].key + data[1].value === 0? 1 :data[1].value],
                        inconsistent: [data[2].key, data[2].key + data[2].value === 0? 1 : data[2].value],
                    });

                    $scope.data = temp[0];
                    //return true;
                }, function(err) {
                    console.error(err);
                    //return false;
                })
            }
            post();
            let interval = setInterval(function() {
                let process = post();
                /*
                if(!process.$$state) {
                    console.log("cleanInterval");
                    clearInterval(interval);
                }
                */
            }, 10 * 1000);

            $scope.$watch("data", function(newValue, oldValue){
               if(newValue === oldValue)
                   return;

                /*console.log("changed!");
                console.log(oldValue);
                console.log(newValue);
                */

                incompleteOption.series[0].data=[{value: $scope.data.incomplete[0], name:'不完整数据'}, {value:$scope.data.incomplete[1], name:'正常数据'}];
                inaccurateOption.series[0].data=[{value: $scope.data.inaccurate[0], name:'不精确数据'}, {value:$scope.data.inaccurate[1], name:'正常数据'}];
                inconsistentOption.series[0].data=[{value: $scope.data.inconsistent[0], name:'不一致列'}, {value:$scope.data.inconsistent[1], name:'正常列'}];

                incompleteChart.setOption(incompleteOption, true);
                inaccurateChart.setOption(inaccurateOption, true);
                inconsistentChart.setOption(inconsistentOption, true);
            }, true);

            // 清空值
            $scope.clear = function() {
                communicationService.post('/api/fan-data-problem/statistics', {
                    operation: "clear"
                }).then(function(data){
                    let temp = [];


                    // console.log("clear");
                    // console.log(data);

                    temp.push({
                        incomplete: [data[0].key, data[0].key + data[0].value === 0? 1 :data[0].value],
                        inaccurate: [data[1].key, data[1].key + data[1].value === 0? 1 :data[1].value],
                        inconsistent: [data[2].key, data[2].key + data[2].value === 0? 1 : data[2].value],
                    });


                    $scope.data = temp[0];

                }, function(err) {
                    console.warn(err);
                });
            };
    }]);

